package com.ruoyi.resume.mapper;

import com.ruoyi.resume.domain.model.Tag;
import com.ruoyi.resume.provider.TagSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TagMapper {

    @SelectProvider(type = TagSqlProvider.class, method = "selectAllTags")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "tagValue", column = "tag_value"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "tagDescription", column = "tag_description"),
            @Result(property = "creator", column = "creator")
    })
    List<Tag> selectAllTags();

    @SelectProvider(type = TagSqlProvider.class, method = "selectTagById")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "tagValue", column = "tag_value"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "tagDescription", column = "tag_description"),
            @Result(property = "creator", column = "creator")
    })
    Tag selectTagById(@Param("id") Integer id);



    @SelectProvider(type = TagSqlProvider.class, method = "selectTagsByCategoryId")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "tagValue", column = "tag_value"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "tagDescription", column = "tag_description"),
            @Result(property = "creator", column = "creator")
    })
    List<Tag> selectTagsByCategoryId(@Param("categoryId") Integer categoryId);

    @Insert("INSERT INTO tag (id, category_id, tag_value, created_at, tag_description, creator) " +
            "VALUES (#{id}, #{categoryId}, #{tagValue}, #{createdAt}, #{tagDescription}, #{creator})")
    int insertTag(Tag tag);



    @Delete("DELETE FROM tag WHERE id = #{id}")
    int deleteTagById(@Param("id") Integer id);

    @Select("SELECT IFNULL(MAX(id), 0) FROM tag")
    int getMaxId();

    @Update("UPDATE tag SET category_id = #{categoryId}, tag_value = #{tagValue}, " +
            "created_at = #{createdAt}, tag_description = #{tagDescription}, creator = #{creator} " +
            "WHERE id = #{id}")
    int updateTag(Tag tag);

    // 根据 category_id 删除所有 tag
    @Delete("DELETE FROM tag WHERE category_id = #{categoryId}")
    int deleteTagsByCategoryId(@Param("categoryId") Integer categoryId);



}
